/**
 * Created by Administrator on 2018/4/3.
 */

layui.use(['layer'], function () {
    var layer = layui.layer;
    initGlobalPage();
    initPage();
    function initPage() {
        $('.go-square-btn').css('left', (winW - 110) / 2 + 'px');

        showNearChallenge(20);
    };

    /**
     * 显示附近挑战
     * @param number 挑战数目
     */
    function showNearChallenge(number) {
        // 百度地图API功能
        var map = new BMap.Map("allmap", {minZoom: 12, maxZoom: 20});
        //禁止双击放大
        map.disableDoubleClickZoom();
        //开启点击监听
        //map.disableDragging();

        var geolocation = new BMap.Geolocation();

        //获取当前位置
        geolocation.getCurrentPosition(function (r) {

            if (r.accuracy) {
                if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                    var mk = new BMap.Marker(r.point);
                    map.addOverlay(mk);
                    map.panTo(r.point);
                    console.log('您的位置：' + r.point.lng + ',' + r.point.lat);
                    var curruntPoint = new BMap.Point(r.point.lng, r.point.lat);
                    map.centerAndZoom(curruntPoint, 17);

                    var token =  sessionStorage.access_token;
                    var data = {
                        token:token,
                        number:number
                    }
                    $.ajax({
                        type: "post"
                        , url: '/index/challenge/ajax_near_challenge_list'
                        , data: data
                        , async: false
                        , success: function (result) {
                            console.info(result);
                            if (result.code == 0) {
                                againToken();
                                return;
                            }
                            if(result.code == 1){
                                $(result.data).each(function(index,value){
                                    var offsetX = (Math.random() - 0.5) / 120;
                                    var offsetY = (Math.random() - 0.5) / 120;
                                    var randomPonit = new BMap.Point(r.point.lng + offsetX, r.point.lat + offsetY);
                                    var myCompOverlay = new ComplexCustomOverlay(randomPonit, value.id,value.money,value.status,value.avatar);
                                    map.addOverlay(myCompOverlay);
                                })
                            }
                            if (result.code == -1) {
                                layer.msg("获取数据失败");
                            }
                        }
                        , error: function (resq) {
                            console.info(resq);
                            layer.msg("服务器异常");
                        }

                    });
                }
                else {
                    alert('failed' + this.getStatus());
                    window.location.href = 'index.html';
                }
            } else {
                window.location.href = 'index.html';
            }
        }, {enableHighAccuracy: true})

        // 复杂的自定义覆盖物
        function ComplexCustomOverlay(point, id, money, type,avatar) {
            this._point = point;
            this._id = id;
            this._money = parseInt(money);
            this._type = type;
            this._avatar = avatar;
        }

        ComplexCustomOverlay.prototype = new BMap.Overlay();
        ComplexCustomOverlay.prototype.initialize = function (map) {
            this._map = map;
            //标注
            var div = this._div = document.createElement("div");
            var typehtml = '';
            switch (this._type) {
                case 0:
                    typehtml = '未领取';
                    div.className = 'make';
                    break;
                case 1:
                    typehtml = '抢答中';
                    div.className = 'make no';
                    break;
                case 2:
                    typehtml = '已撤回';
                    div.className = 'make no';
                    break;
                case 3:
                    typehtml = '已领取';
                    div.className = 'make no';
                    break;
                default:
                    typehtml = '已领取';
                    div.className = 'make no';
            }


            div.id = this._id;
            div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat + 1);


            //头像
            var img = document.createElement("img");
            img.src = this._avatar;
            div.appendChild(img);

            //金额
            var qian = document.createElement("span");
            qian.className = 'qian';
            qian.appendChild(document.createTextNode(this._money + '元挑战'));
            div.appendChild(qian);

            //状态
            var type = document.createElement("span");
            type.className = 'type';

            type.appendChild(document.createTextNode(typehtml));
            div.appendChild(type);


            div.addEventListener("touchend", function () {
                window.location.href = 'answer.html?id='+div.id;
            });
            map.getPanes().labelPane.appendChild(div);
            return div;
        }
        ComplexCustomOverlay.prototype.draw = function () {
            var map = this._map;
            var pixel = map.pointToOverlayPixel(this._point);
            this._div.style.left = pixel.x - 35 + "px";
            this._div.style.top = pixel.y - 95 + "px";
        }
    }
});





